﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core.Persistence;
using Umbraco.Web.Editors;
using Umbraco.Web.Mvc;

using UmbracoExtending.District.Model;

namespace UmbracoExtending.District.Controller
{
    /// <summary>
    /// Umbraco集成后端行政区划控制器
    /// </summary>
    [PluginController("UmbracoDistrict")]
    public class BackendDistrictController : UmbracoAuthorizedJsonController
    {
        /// <summary>
        /// 获取全部省份信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DistrictItem> GetProvince()
        {
            var db = UmbracoContext.Application.DatabaseContext.Database;
            var query = new Sql().Select("[DistrictCode] as Code, [DistrictName] as Name")
                .From("[umbracoExtendingDistrict]")
                .Where("[ProvinceCode] = @0", "000000")
                .OrderBy("[DistrictCode]");

            return db.Fetch<DistrictItem>(query);
        }

        /// <summary>
        /// 获取城市数据
        /// </summary>
        /// <param name="province"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public IEnumerable<DistrictItem> GetCity(string province, int level)
        {
            var db = UmbracoContext.Application.DatabaseContext.Database;
            var query = new Sql().Select("[DistrictCode] as Code, [DistrictName] as Name")
                .From("[umbracoExtendingDistrict]")
                .Where("[ProvinceCode] = @0 AND [CityCode] = @1", province, "000000");

            //对于不显示三级的，同时城市仅有一个的，作为直辖市特殊处理
            if (level == 0)
            {
                var citys = db.Fetch<DistrictItem>(query);
                if (citys.Count == 1)
                {
                    query = new Sql().Select("[DistrictCode] as Code, [DistrictName] as Name")
                        .From("[umbracoExtendingDistrict]")
                        .Where("[ProvinceCode] = @0 AND [CityCode] = @1", province, citys.FirstOrDefault().Code)
                        .OrderBy("[DistrictCode]");
                    return db.Fetch<DistrictItem>(query);
                }
            }

            return db.Fetch<DistrictItem>(query.OrderBy("[DistrictCode]"));
        }

        /// <summary>
        /// 获取三级的地区
        /// </summary>
        /// <param name="city">城市编号</param>
        /// <returns></returns>
        public IEnumerable<DistrictItem> GetDistrict(string city)
        {
            if (city.Equals("000000"))
            {
                return new List<DistrictItem>();
            }
            var db = UmbracoContext.Application.DatabaseContext.Database;
            var query = new Sql().Select("[DistrictCode] as Code, [DistrictName] as Name")
                .From("[umbracoExtendingDistrict]")
                .Where("[CityCode] = @0", city)
                .OrderBy("[DistrictCode]");

            return db.Fetch<DistrictItem>(query);
        }

        /// <summary>
        /// 获取某个具体的地区信息的对象
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public DistrictInfo GetOne(string code)
        {
            var db = UmbracoContext.Application.DatabaseContext.Database;
            var query = new Sql().Select("ProvinceCode, ProvinceName, CityCode, CityName, DistrictCode, DistrictName, AliasName")
                .From("[umbracoExtendingDistrict]")
                .Where("[DistrictCode] = @0", code);
            return db.SingleOrDefault<DistrictInfo>(query);
        }
    }
}
